package com.zhuimeng.Application.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuimeng.Application.dao.userDao;
import com.zhuimeng.Application.entity.user;
import com.zhuimeng.Application.utils.MapCodeUils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
@Primary
public class UserDBService implements IUserSelvice {

    @Resource
    private userDao userDao;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public int regist(user user) {
        int count;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        count = userDao.selectCount(queryWrapper);
        if (count > 0) {
            return -1;
        }
        queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", user.getPhone());
        count = userDao.selectCount(queryWrapper);
        if (count > 0) {
            return -2;
        }
        userDao.insert(user);
        return 1;
    }

    @Override
    public user login(String username, String userpassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("userpassword", userpassword);
        user user1 = userDao.selectOne(queryWrapper);
        return user1;
    }

    @Override
    public int sendEmailByUserName(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        int count = userDao.selectCount(queryWrapper);
        if (count <= 0) {
            return -1;
        }

        user user = userDao.selectOne(queryWrapper);

        int code = (int) (Math.random() * 9000 + 1000);

        MapCodeUils.putCode(username, code + "");

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        try {
            mimeMessageHelper.setSubject("找回密码");
            mimeMessageHelper.setFrom("ceng6228841107@163.com");
            mimeMessageHelper.setTo(user.getEmail());
            mimeMessageHelper.setText("验证码：" + code + ",十分钟内有效");
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return 1;
    }

    /**
     * 根据用户名重置密码
     *
     * @param username
     * @param newPassword
     * @return
     */
    @Override
    public int updataPassword(String username, String newPassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        user user = userDao.selectOne(queryWrapper);
        user.setUserpassword(newPassword);
        MapCodeUils.removeCode(username);
        int result = userDao.updateById(user);
        return result;
    }

    @Override
    public int updataHander(String username, String header) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        user user = userDao.selectOne(queryWrapper);
        user.setHeader(header);
        int result = userDao.updateById(user);
        return result;
    }

    @Override
    public int updataHappyBean(Integer uid, Integer happyBean) {
        user user=userDao.selectById(uid);

        user.setHappyBean(user.getHappyBean()+happyBean);

        userDao.updateById(user);

        return user.getHappyBean();
    }

}
